//
//  TweetListViewModel.m
//  OSChina
//
//  Created by 8b410-2 on 16/12/25.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCTweetListViewModel.h"
#import "OSCTweetTools.h"
#import "OSCTweet.h"
#import "OSCTweetDetailViewModel.h"
#import "OSCTweetViewModel.h"
#import "OSCTweetListRequestParams.h"
#import "OSCTweetDetailWithBottomBarViewModel.h"
#import <SDWebImage/SDWebImageManager.h>
@interface OSCTweetListViewModel ()
/// 动弹列表类型
@property (nonatomic, assign) NSInteger typeNum;
/// 动弹列表请求参数
@property (nonatomic, strong, readwrite) OSCTweetListRequestParams *listParams;

@property (nonatomic, assign, readwrite) OSCTweetsType type;

@end

@implementation OSCTweetListViewModel

@dynamic listParams;

- (instancetype)initWithServices:(id<OSCViewModelServices>)services params:(NSDictionary *)params {
    self = [super initWithServices:services params:params];
    if (self) {
        OSCTweetsType type = [[params objectForKey:@"OSCTweetsType"] integerValue];
        self.type = type;
        switch (type) {
            case OSCTweetsTypeNew:
                _typeNum = 0;
                break;
            case OSCTweetsTypeHot:
                _typeNum = -1;
                break;
            case OSCTweetsTypeMy:
                [RACObserve([OSCAccountTools shareSession], account) subscribeNext:^(OSCAccount *account) {
                    _typeNum = account.user.uid;
                }];
                break;
        }
        self.listParams = [[OSCTweetListRequestParams alloc] initWithTypeNum:self.typeNum];
    }
    return self;
}

- (void)initialize {
    [super initialize];
    
    self.dataSourceSignal = [self.requestListDataCommand.executionSignals.flatten map:^id(NSArray *models) {
        // 定义viewModel数组
        NSMutableArray *cellViewModels = [[NSMutableArray alloc] init];
        for (OSCTweet *model in models) {
            // 转viewModel
            OSCTweetViewModel *cellViewModel = [[OSCTweetViewModel alloc] initWithModel:model];
            [cellViewModels addObject:cellViewModel];
        }
        return cellViewModels;
    }];
    
    [self.requestListDataCommand.errors subscribe:self.errors];
    
    @weakify(self);
    self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(OSCTweetViewModel *tweetViewModel) {
        @strongify(self);
        OSCTweetDetailViewModel *childViewModel = [[OSCTweetDetailViewModel alloc]
                                                   initWithServices:self.services
                                                   params:@{@"tweetViewModel" :tweetViewModel,
                                                            @"title" :@"动弹详情"}];
        OSCTweetDetailWithBottomBarViewModel *viewModel = [[OSCTweetDetailWithBottomBarViewModel alloc]
                                                           initWithServices:self.services
                                                           params:@{@"childViewModel" :childViewModel,
                                                                    @"title" :@"动弹详情"}];
        [self.services pushViewModel:viewModel animated:YES];
        return [RACSignal empty];
    }];
}

- (RACSignal *)requestListDataSignal {
    return [OSCTweetTools requestTweetListWithParamsModel:self.listParams];
}

@end
